package com.gaga.console.core.controller.sys;


import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gaga.common.enums.DelFlag;
import com.gaga.common.path.ApiPath;
import com.gaga.common.result.Result;
import com.gaga.console.base.controller.BaseController;
import com.gaga.console.base.entity.PageParams;
import com.gaga.console.base.mybatisplus.LambdaQueryWrapperExt;
import com.gaga.console.core.entity.SysDict;
import com.gaga.console.core.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 字典表 前端控制器
 *
 * @author liujia
 * @since 2022-07-14
 */
@Api(tags = "系统模块-字典管理")
@RestController
@RequestMapping(ApiPath.ROOT + "/sys/dict")
public class SysDictController extends BaseController {
    @Autowired
    private SysDictService sysDictService;

    /**
     * 获取列表
     */
    @ApiOperation(value = "查询list")
    @PostMapping("/list")
    public Result list(@RequestBody PageParams<SysDict> data) {

        LambdaQueryWrapperExt<SysDict> wraps = new LambdaQueryWrapperExt<>();
        wraps.eq(SysDict::getDelFlag, DelFlag.OK.getCode());
        data.buildSpecWrap(wraps, SysDict.class);

        IPage<SysDict> roleIPage = sysDictService.page(data.buildPage(), wraps);

        return Result.ok(data.buildTableResult(roleIPage));
    }


    /**
     * 获取列表
     */
    @ApiOperation(value = "查询dictList")
    @PostMapping("/dictList")
    public Result dictList(@RequestBody PageParams<SysDict> data) {

        LambdaQueryWrapperExt<SysDict> wraps = new LambdaQueryWrapperExt<>();
        wraps.eq(SysDict::getDelFlag, DelFlag.OK.getCode()).ne(SysDict::getParentId, 0);
        data.buildSpecWrap(wraps, SysDict.class);

        return Result.ok(sysDictService.list(wraps));
    }

    /**
     * 根据详细信息
     */
    @ApiOperation(value = "获取单个记录")
    @GetMapping(value = "/{id}")
    public Result getEntity(@PathVariable Long id) {
        return Result.ok(sysDictService.getById(id));
    }


    /**
     * 保存
     */
    @ApiOperation(value = "保存记录")
    @PostMapping("/save")
    public Result save(@Validated @RequestBody SysDict entity) {
        return toAjaxBoolean(sysDictService.saveOrUpdate(entity));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除记录")
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Integer[] ids) {

        if (ArrayUtil.isEmpty(ids)) {
            return toAjaxBoolean(false);
        }

        List<SysDict> removeEntityList = Arrays.stream(ids)
                .map(id -> {
                    SysDict entity = new SysDict().setId(id);
                    entity.setDelFlag(DelFlag.DELETED.getCode());
                    entity.setDeleteTime(new Date());
                    return entity;
                }).collect(Collectors.toList());

        return toAjaxBoolean(sysDictService.updateBatchById(removeEntityList));
    }


}
